package com.ninth_group.utils;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UpToHiveUtil {
    // 上传数据命令前缀
    private static final String LOAD_DATA_SQL_PREFIX =
            "LOAD DATA INPATH '";
    // 上传数据命令后缀
    private static final String LOAD_DATA_SQL_SUFFIX =
            "' OVERWRITE INTO TABLE ";

    @Resource
    private HiveUtil hiveUtil;

    /**
     * 将类路径下的csv添加数据到hive表
     * @param localPath 类路径下的csv文件路径
     * @param hdfsPath hdfs csv文件路径
     * @param tableName hive表名
     */
    public void addDate(String localPath, String hdfsPath, String tableName) {
        try {
            HdfsUploader.upData(localPath, hdfsPath); // 上传数据到HDFS
            System.out.println("上传代码: " + LOAD_DATA_SQL_PREFIX +  hdfsPath +
                    localPath + LOAD_DATA_SQL_SUFFIX + tableName);
            hiveUtil.executeUpdate(LOAD_DATA_SQL_PREFIX +  hdfsPath +
                    localPath + LOAD_DATA_SQL_SUFFIX + tableName); // 加载数据到Hive

        } catch (Exception e) {
            System.err.println("上传失败:");
            e.printStackTrace();
        }
    }

}
